﻿/*
1.OpenXmlSDK是个很好的类库，可惜只能通过C＃调用，C＃的童鞋又福气了。
2.服务端程序由于没法安装office,所以这个对asp.net网站来说是最理想的库了。需要.net 4.0版本以上.

 采用 Open XML SDK方式 访问EXCEL文件，实测比NPOI方式快很多，但仅适用于2007以上版本
这种方式在Msdn上有详细的解释，http://msdn.microsoft.com/zh-cn/library/office/cc850837(v=office.15).aspx，可参考msdn sdk。这种方式只能处理2007以上的版本，不支持2003。 

通过NuGet搜索 DocumentFormat.OpenXmlSDK（注意是SDK，比较小，不知道功能上有什么缺失。同是微软出品，为什么出一个非官方版？）

2007Excel版本的文件是多个XML文件的压缩包，其中需要注意的几个文件styles.xml、sharedStrings.xml、workbook.xml、worksheets中各个sheet。 
styles.xml:主要用来存放Excel中样式的(包括格式化的数据格式，如日期、货币等等)； 
sharedStrings.xml:主要存放共享的String数据的，在没有对Excel中相关单元格和数据格式化都可以通过这个文件读取； 
workbook.xml:主要存放工作簿中各个工作表的命名和命名空间，在获取各个工作表的名称可以通过寻址找到节点，获取各表名称； 
参考：
https://blog.csdn.net/dapengbusi/article/details/38118679
 */
using CC.Common;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace OpenXmlSdk
{
    public static class OpenXmlExcelReader
    {
        #region Public Functions
        /// <summary>
        /// 获取Excel中多表的表名
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<string> GetSheetNames(string filePath)
        {
            string sheetName = string.Empty;
            List<string> sheetNames = new List<string>();//所有Sheet表名
            using (SpreadsheetDocument spreadDocument = SpreadsheetDocument.Open(filePath, false))
            {
                WorkbookPart workBook = spreadDocument.WorkbookPart;
                Stream stream = workBook.GetStream(FileMode.Open);
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(stream);

                XmlNamespaceManager xmlNSManager = new XmlNamespaceManager(xmlDocument.NameTable);
                xmlNSManager.AddNamespace("default", xmlDocument.DocumentElement.NamespaceURI);
                XmlNodeList nodeList = xmlDocument.SelectNodes("//default:sheets/default:sheet", xmlNSManager);

                foreach (XmlNode node in nodeList)
                {
                    sheetName = node.Attributes["name"].Value;
                    sheetNames.Add(sheetName);
                }
            }
            return sheetNames;
        }
        /// <summary>
        /// 将Excel单一表转化为DataTable对象
        /// </summary>
        /// <param name="sheetName">SheetName</param>
        /// <param name="stream">Excel文件路径</param>
        /// <returns>DataTable对象</returns>
        public static DataTable ExcelToDataTable(string filePath, string sheetName)
        {
            DataTable dataTable = new DataTable();
            //根据Excel流转换为spreadDocument对象
            using (SpreadsheetDocument spreadDocument = SpreadsheetDocument.Open(filePath, false))//Excel文档包
            {
                //Workbook workBook = spreadDocument.WorkbookPart.Workbook;//主文档部件的根元素
                //Sheets sheeets = workBook.Sheets;//块级结构（如工作表、文件版本等）的容器
                WorkbookPart workBookPart = spreadDocument.WorkbookPart;
                //获取Excel中SheetName集合
                List<string> sheetNames = GetSheetNames(workBookPart);

                if (sheetNames.Contains(sheetName))
                {
                    //根据WorkSheet转化为Table
                    dataTable = WorkSheetToTable(workBookPart, sheetName);
                }
            }
            return dataTable;
        }
        /// <summary>
        /// 将Excel多单一表转化为DataSet数据集对象
        /// </summary>
        /// <param name="filePath">Excel文件路径</param>
        /// <returns>转化的数据集，每个页面对应一个DataTable</returns>
        public static DataSet ExcelToDataSet(string filePath)
        {
            DataSet dataSet = new DataSet();
            using (SpreadsheetDocument spreadDocument = SpreadsheetDocument.Open(filePath, false))
            {
                //指定WorkbookPart对象
                WorkbookPart workBookPart = spreadDocument.WorkbookPart;
                //获取Excel中SheetName集合
                List<string> sheetNames = GetSheetNames(workBookPart);

                foreach (string sheetName in sheetNames)
                {
                    DataTable dataTable = WorkSheetToTable(workBookPart, sheetName);
                    if (dataTable != null)
                    {
                        dataSet.Tables.Add(dataTable);//将表添加到数据集
                    }
                }
            }
            return dataSet;
        }

        /// <summary>
        /// 将Excel中的数据映射实体对象集合,支持DataColumnAttribute
        /// 1.第一行作为列标题，程序把列标题对应的值放入同名的实体属性中
        /// 2.在读取数据时，读出成string型数据，然后根据属性的类型，解释成对应的数据类型
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="sheetName">操作SheetName</param>
        /// <returns>实体对象集合</returns>
        public static List<T> ExcelToList<T>(string filePath, string sheetName)
        {
            List<T> list = new List<T>();
            using (SpreadsheetDocument spreadDocument = SpreadsheetDocument.Open(filePath, false))//Excel文档包
            {
                WorkbookPart workBookPart = spreadDocument.WorkbookPart;
                list = ExtcelToObjects<T>(workBookPart, sheetName);
            }
            return list;
        }

       
        #endregion




        /// <summary>
        /// 根据WorkbookPart获取所有SheetName
        /// </summary>
        /// <param name="workBookPart"></param>
        /// <returns>SheetName集合</returns>
        private static List<string> GetSheetNames(WorkbookPart workBookPart)
        {
            List<string> sheetNames = new List<string>();
            Sheets sheets = workBookPart.Workbook.Sheets;
            foreach (Sheet sheet in sheets)
            {
                string sheetName = sheet.Name;
                if (!string.IsNullOrEmpty(sheetName))
                {
                    sheetNames.Add(sheetName);
                }
            }
            return sheetNames;
        }

        /// <summary>
        /// 根据WorkbookPart和sheetName获取该Sheet下所有Row数据
        /// </summary>
        /// <param name="workBookPart">WorkbookPart对象</param>
        /// <param name="sheetName">SheetName</param>
        /// <returns>该SheetName下的所有Row数据</returns>
        private static IEnumerable<Row> GetWorkBookPartRows(WorkbookPart workBookPart, string sheetName)
        {
            IEnumerable<Row> sheetRows = null;
            //根据表名在WorkbookPart中获取Sheet集合
            IEnumerable<Sheet> sheets = workBookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName);
            if (sheets.Count() == 0)
            {
                return null;//没有数据
            }

            WorksheetPart workSheetPart = workBookPart.GetPartById(sheets.First().Id) as WorksheetPart;
            //获取Excel中得到的行
            sheetRows = workSheetPart.Worksheet.Descendants<Row>();

            return sheetRows;
        }

        /// <summary>
        /// 根据WorkbookPart和表名创建DataTable对象
        /// </summary>
        /// <param name="workBookPart">WorkbookPart对象</param>
        /// <param name="tableName">表名</param>
        /// <returns>转化后的DataTable</returns>
        private static DataTable WorkSheetToTable(WorkbookPart workBookPart, string sheetName)
        {
            //创建Table
            DataTable dataTable = new DataTable(sheetName);

            //根据WorkbookPart和sheetName获取该Sheet下所有行数据
            IEnumerable<Row> sheetRows = GetWorkBookPartRows(workBookPart, sheetName);
            if (sheetRows == null || sheetRows.Count() <= 0)
            {
                return null;
            }

            //将数据导入DataTable,假定第一行为列名,第二行以后为数据
            foreach (Row row in sheetRows)
            {
                //获取Excel中的列头
                if (row.RowIndex == 1)
                {
                    List<DataColumn> listCols = GetDataColumn(row, workBookPart);
                    dataTable.Columns.AddRange(listCols.ToArray());
                }
                else
                {
                    //Excel第二行同时为DataTable的第一行数据
                    DataRow dataRow = GetDataRow(row, dataTable, workBookPart);
                    if (dataRow != null)
                    {
                        dataTable.Rows.Add(dataRow);
                    }
                }
            }
            return dataTable;
        }

        /// <summary>
        /// 根据WorkbookPart获取NumberingFormats样式集合
        /// </summary>
        /// <param name="workBookPart">WorkbookPart对象</param>
        /// <returns>NumberingFormats样式集合</returns>
        private static List<string> GetNumberFormatsStyle(WorkbookPart workBookPart)
        {
            if (workBookPart.WorkbookStylesPart == null) return null;
            List<string> dicStyle = new List<string>();
            Stylesheet styleSheet = workBookPart.WorkbookStylesPart.Stylesheet;
            if(styleSheet.NumberingFormats!=null)
            {
                OpenXmlElementList list = styleSheet.NumberingFormats.ChildElements;//获取NumberingFormats样式集合

                foreach (var element in list)//格式化节点
                {
                    if (element.HasAttributes)
                    {
                        using (DocumentFormat.OpenXml.OpenXmlReader reader = DocumentFormat.OpenXml.OpenXmlReader.Create(element))
                        {
                            if (reader.Read())
                            {
                                if (reader.Attributes.Count > 0)
                                {
                                    string numFmtId = reader.Attributes[0].Value;//格式化ID
                                    string formatCode = reader.Attributes[1].Value;//格式化Code
                                    dicStyle.Add(formatCode);//将格式化Code写入List集合
                                }
                            }
                        }
                    }
                }
            }
            
            return dicStyle;
        }

        /// <summary>
        /// 根据行对象和WorkbookPart对象获取DataColumn集合
        /// </summary>
        /// <param name="row">Excel中行记录</param>
        /// <param name="workBookPart">WorkbookPart对象</param>
        /// <returns>返回DataColumn对象集合</returns>
        private static List<DataColumn> GetDataColumn(Row row, WorkbookPart workBookPart)
        {
            List<DataColumn> listCols = new List<DataColumn>();
            foreach (Cell cell in row)
            {
                string cellValue = GetCellValue(workBookPart,cell);
                DataColumn col = new DataColumn(cellValue);
                listCols.Add(col);
            }
            return listCols;
        }

        /// <summary>
        /// 根据Excel行\数据库表\WorkbookPart对象获取数据DataRow
        /// </summary>
        /// <param name="row">Excel中行对象</param>
        /// <param name="dateTable">数据表</param>
        /// <param name="workBookPart">WorkbookPart对象</param>
        /// <returns>返回一条数据记录</returns>
        private static DataRow GetDataRow(Row row, DataTable dateTable, WorkbookPart workBookPart)
        {
            //读取Excel中数据,一一读取单元格,若整行为空则忽视该行
            DataRow dataRow = dateTable.NewRow();
            IEnumerable<Cell> cells = row.Elements<Cell>();

            int cellIndex = 0;//单元格索引
            int nullCellCount = cellIndex;//空行索引
            foreach (Cell cell in row)
            {
                object cellVlue = GetCellObjValue(workBookPart,cell);
               
                dataRow[cellIndex] = cellVlue;
                cellIndex++;
            }
            if (nullCellCount == cellIndex)//剔除空行
            {
                dataRow = null;//一行中单元格索引和空行索引一样
            }
            return dataRow;
        }
        /// <summary>
        /// 根据Excel单元格和WorkbookPart对象获取单元格的值
        /// 问题：如果为日期型数据，但没有在excel中设置过日期格式的话，会被识别为数值
        /// </summary>
        /// <param name="cell">Excel单元格对象</param>
        /// <param name="workBookPart">Excel WorkbookPart对象</param>
        /// <returns>单元格的值</returns>
        private static object GetCellObjValue( WorkbookPart workBookPart, Cell cell)
        {
            object cellValue = null;
            if (cell.ChildElements.Count == 0)//Cell节点下没有子节点
            {
                return cellValue;
            }
            //string cellRefId = cell.CellReference.InnerText;//获取引用相对位置
            string cellInnerText = cell.CellValue.InnerText;//获取Cell的InnerText
            //获取WorkbookPart中NumberingFormats样式集合
            List<string> dicStyles = GetNumberFormatsStyle(workBookPart);
            EnumValue<CellValues> cellType = cell.DataType;//获取Cell数据类型
            if (cellType == null)//指示单元格内的数据类型。 对于数字和日期类型， DataType 属性的值为 null
            {
                if (dicStyles!=null&& dicStyles.Count > 0 && cell.StyleIndex != null)//对于数字,cell.StyleIndex==null
                {
                    int styleIndex = Convert.ToInt32(cell.StyleIndex.Value);
                    if(styleIndex<=dicStyles.Count)
                    {
                        string cellStyle = dicStyles[styleIndex - 1];//获取该索引的样式
                        if (cellStyle.Contains("yyyy") || cellStyle.Contains("h")
                            || cellStyle.Contains("dd") || cellStyle.Contains("ss"))
                        {
                            //如果为日期或时间进行格式处理,去掉“;@”
                            cellStyle = cellStyle.Replace(";@", "");
                            while (cellStyle.Contains("[") && cellStyle.Contains("]"))
                            {
                                int otherStart = cellStyle.IndexOf('[');
                                int otherEnd = cellStyle.IndexOf("]");

                                cellStyle = cellStyle.Remove(otherStart, otherEnd - otherStart + 1);
                            }
                            double doubleDateTime = double.Parse(cellInnerText);
                            cellValue = DateTime.FromOADate(doubleDateTime);//将Double日期数字转为日期格式

                        }
                        else//其他的货币、数值
                        {
                            cellStyle = cellStyle.Substring(cellStyle.LastIndexOf('.') - 1).Replace("\\", "");
                            decimal decimalNum = decimal.Parse(cellInnerText);
                            cellValue = decimal.Parse(decimalNum.ToString(cellStyle));
                        }
                    }
                    else
                    {
                        cellValue = double.Parse(cellInnerText);
                    }
                }
                else
                {
                    cellValue = double.Parse(cellInnerText);
                }
            }
            else//字符串和布尔值, DataType!=null
            {
                //获取WorkbookPart中共享String数据
                SharedStringTable sharedTable = workBookPart.SharedStringTablePart.SharedStringTable;
                switch (cellType.Value)
                {
                    case CellValues.SharedString://字符串
                                                 //获取该Cell的所在的索引
                        int cellIndex = int.Parse(cellInnerText);
                        cellValue = sharedTable.ChildElements[cellIndex].InnerText;
                        break;
                    case CellValues.Boolean://布尔
                        cellValue = (cellInnerText == "1");
                        break;
                    case CellValues.Date://日期
                        cellValue = Convert.ToDateTime(cellInnerText);
                        break;
                    case CellValues.Number://数字
                        cellValue = Convert.ToDecimal(cellInnerText);
                        break;
                    default: 
                        cellValue = cellInnerText; 
                        break;
                }
            }
            return cellValue;
        }
        /// <summary>
        /// 根据Excel单元格和WorkbookPart对象获取单元格的值
        /// </summary>
        /// <param name="cell">Excel单元格对象</param>
        /// <param name="workBookPart">Excel WorkbookPart对象</param>
        /// <returns>单元格的值</returns>
        public static string GetCellValue2(WorkbookPart workBookPart,Cell cell)
        {
            string cellValue = string.Empty;
            if (cell.ChildElements.Count == 0)//Cell节点下没有子节点
            {
                return cellValue;
            }
            string cellRefId = cell.CellReference.InnerText;//获取引用相对位置
            string cellInnerText = cell.CellValue.InnerText;//获取Cell的InnerText
            cellValue = cellInnerText;//指定默认值(其实用来处理Excel中的数字)

            //获取WorkbookPart中NumberingFormats样式集合
            List<string> dicStyles = GetNumberFormatsStyle(workBookPart);
            //获取WorkbookPart中共享String数据
            SharedStringTable sharedTable = workBookPart.SharedStringTablePart.SharedStringTable;

            try
            {
                
                if (cell.DataType != null)//对于数字和日期类型， DataType 属性的值为 null
                {
                    switch (cell.DataType.Value)
                    {
                        case CellValues.SharedString://字符串
                            //获取该Cell的所在的索引
                            int cellIndex = int.Parse(cellInnerText);
                            cellValue = sharedTable.ChildElements[cellIndex].InnerText;
                            break;
                        case CellValues.Boolean://布尔
                            cellValue = (cellInnerText == "1") ? "TRUE" : "FALSE";
                            break;
                        case CellValues.Date://日期
                            cellValue = Convert.ToDateTime(cellInnerText).ToString();
                            break;
                        case CellValues.Number://数字
                            cellValue = Convert.ToDecimal(cellInnerText).ToString();
                            break;
                        default: cellValue = cellInnerText; break;
                    }
                }
                else//格式化数据
                {
                    if (dicStyles!=null && dicStyles.Count > 0 && cell.StyleIndex != null)//对于数字,cell.StyleIndex==null
                    {
                        int styleIndex = Convert.ToInt32(cell.StyleIndex.Value);
                        string cellStyle = dicStyles[styleIndex - 1];//获取该索引的样式
                        if (cellStyle.Contains("yyyy") || cellStyle.Contains("h")
                            || cellStyle.Contains("dd") || cellStyle.Contains("ss"))
                        {
                            //如果为日期或时间进行格式处理,去掉“;@”
                            cellStyle = cellStyle.Replace(";@", "");
                            while (cellStyle.Contains("[") && cellStyle.Contains("]"))
                            {
                                int otherStart = cellStyle.IndexOf('[');
                                int otherEnd = cellStyle.IndexOf("]");

                                cellStyle = cellStyle.Remove(otherStart, otherEnd - otherStart + 1);
                            }
                            double doubleDateTime = double.Parse(cellInnerText);
                            DateTime dateTime = DateTime.FromOADate(doubleDateTime);//将Double日期数字转为日期格式
                            if (cellStyle.Contains("m")) { cellStyle = cellStyle.Replace("m", "M"); }
                            if (cellStyle.Contains("AM/PM")) { cellStyle = cellStyle.Replace("AM/PM", ""); }
                            cellValue = dateTime.ToString(cellStyle);//不知道为什么Excel 2007中格式日期为yyyy/m/d
                        }
                        else//其他的货币、数值
                        {
                            cellStyle = cellStyle.Substring(cellStyle.LastIndexOf('.') - 1).Replace("\\", "");
                            decimal decimalNum = decimal.Parse(cellInnerText);
                            cellValue = decimal.Parse(decimalNum.ToString(cellStyle)).ToString();
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                //string expMessage = string.Format("Excel中{0}位置数据有误,请确认填写正确！", cellRefId);
                //throw new Exception(expMessage);
                cellValue = "N/A";
            }
            return cellValue;
        }

        private static string GetCellValue(WorkbookPart workBookPart, Cell cell)
        {
            SharedStringTablePart stringTablePart = workBookPart.SharedStringTablePart;
            if (cell.CellValue == null) return "";
            string value = cell.CellValue.InnerXml;
            EnumValue<CellValues> cellType = cell.DataType;//获取Cell数据类型
            if (cell.DataType != null && (cell.DataType.Value == CellValues.SharedString || cell.DataType.Value == CellValues.String || cell.DataType.Value == CellValues.Number))
            {
                return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
            }
            else //浮点数和日期对应的cell.DataType都为NULL
            {
                // DateTime.FromOADate((double.Parse(value)); 如果确定是日期就可以直接用过该方法转换为日期对象，可是无法确定DataType==NULL的时候这个CELL 数据到底是浮点型还是日期.(日期被自动转换为浮点
                return value;
            }
        }


        #region SaveCell
       
       
        private static EnumValue<CellValues> GetDataType(Type type)
        {
            EnumValue<CellValues> dataType;
            switch (type.Name)
            {
                case "System.DateTime":
                case "System.Nullable`1[System.DateTime]":
                    dataType = new EnumValue<CellValues>(CellValues.Date);
                    break;

                case "System.Boolean":
                case "System.Nullable`1[System.Boolean]":
                    dataType = new EnumValue<CellValues>(CellValues.Boolean);
                    break;

                case "System.Byte":
                case "System.Nullable`1[System.Byte]":
                case "System.Int16":
                case "System.Nullable`1[System.Int16]":
                case "System.Int32":
                case "System.Nullable`1[System.Int32]":
                case "System.Int64":
                case "System.Nullable`1[System.Int64]":
                case "System.Double":
                case "System.Nullable`1[System.Double]":
                case "System.Single":
                case "System.Nullable`1[System.Single]":
                case "System.Decimal":
                case "System.Nullable`1[System.Decimal]":
                    dataType = new EnumValue<CellValues>(CellValues.Number);
                    break;

                case "System.String":
                case "System.DBNull":
                default:
                    dataType = new EnumValue<CellValues>(CellValues.String);
                    break;
            }
            return dataType;
        }
       
        private static Cell ReturnCell(Worksheet worksheet, string columnName, uint row)
        {
            Row targetRow = ReturnRow(worksheet, row);

            if (targetRow == null)
                return null;

            return targetRow.Elements<Cell>().Where(c =>
               string.Compare(c.CellReference.Value, columnName + row,
               true) == 0).First();
        }
        private static Row ReturnRow(Worksheet worksheet, uint row)
        {
            return worksheet.GetFirstChild<SheetData>().
            Elements<Row>().Where(r => r.RowIndex == row).First();
        }
        #endregion

        #region Object Exchange

        /// <summary>
        /// 根据WorkbookPart和SheetName获取实体对象集合
        /// 支持DataColumnAttribute
        /// </summary>
        /// <param name="workBookPart">WorkbookPart对象</param>
        /// <param name="sheetName">sheetName</param>
        /// <returns>实体对象集合</returns>
        private static List<T> ExtcelToObjects<T>(WorkbookPart workBookPart, string sheetName)
        {
            List<T> list = new List<T>();
            List<string> columnValues = new List<string>();//列头值集合
            List<string> rowCellValues = null;//行数据集合

            //获取WorkbookPart下名为sheetName的Sheet的所有行数据
            IEnumerable<Row> sheetRows = OpenXmlExcelReader.GetWorkBookPartRows(workBookPart, sheetName);
            if (sheetRows == null || sheetRows.Count() <= 0)
            {
                return null;
            }

            //将数据导入DataTable,假定第一行为列名,第二行以后为数据
            foreach (Row row in sheetRows)
            {
                rowCellValues = new List<string>();//新行数据
                foreach (Cell cell in row)
                {
                    //获取单元格的值
                    string cellValue = GetCellValue2(workBookPart, cell);
                    if (row.RowIndex == 1)
                    {
                        columnValues.Add(cellValue);
                    }
                    else
                    {
                        rowCellValues.Add(cellValue);
                    }
                }
                if (row.RowIndex > 1)
                {
                   
                    T obj = ConvertToObject<T>( columnValues, rowCellValues);
                    list.Add(obj);
                }
            }
            return list;
        }

        /// <summary>
        /// 根据列头集合\行数据集合,转换数据实体对象 
        /// 支持DataColumnAttribute
        /// </summary>
        /// <typeparam name="T">映射实体对象</typeparam>
        /// <param name="columnNames">列头集合</param>
        /// <param name="rowCellValues">行数据集合</param>
        /// <returns>实体对象</returns>
        private static T ConvertToObject<T>( List<string> columnNames, List<string> rowCellValues)
        {
           
            T obj = Activator.CreateInstance<T>();
            for (int index = 0; index < columnNames.Count; index++)
            {
                string colName = columnNames[index];
                var pi = DataColumnAttribute.GetPropertyInfo<T>(colName);
                if (colName.Equals(columnNames[index], StringComparison.OrdinalIgnoreCase))
                {
                    String propertyType = pi.PropertyType.Name;

                    switch (propertyType)
                    {
                        case "Int32":
                            pi.SetValue(obj, int.Parse(rowCellValues[index]), null);
                            break;
                        case "DateTime":
                            pi.SetValue(obj, DateTime.Parse(rowCellValues[index]), null);
                            break;
                        case "Decimal":
                            pi.SetValue(obj, Decimal.Parse(rowCellValues[index]), null);
                            break;
                        case "Double":
                            pi.SetValue(obj, Double.Parse(rowCellValues[index]), null);
                            break;
                        case "String":
                            pi.SetValue(obj, rowCellValues[index], null);
                            break;
                        case "Boolean":
                            pi.SetValue(obj, Boolean.Parse(rowCellValues[index]), null);
                            break;
                    }
                    break;
                }

            }
           
            return obj;
        }
        #endregion
    }
}
   
